package cn.gok.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

interface Fly{
    void fly();
}
class A extends Object implements Fly,Runnable{
    private int x;
    public int y;
    public String str;

    public A() {
    }

    public A(int x) {
        this.x = x;
    }
    public void test(){

    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    @Override
    public void fly() {
        System.out.println("a在飞");
    }

    @Override
    public void run() {

    }
    private void doTest(String str, Float f){
        System.out.println("这是私有方法！str="+str+",f="+f);
    }
}
class B extends A{
    @Override
    public void fly() {
        System.out.println("b在飞");
    }
}
public class Class类例子 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        //class类的加载();
        获取类的信息();
    }

    private static void 获取类的信息() throws NoSuchMethodException {
        Class<A> c = A.class;
        //1获取方法和成员变量  getMethod()//包含父类方法
        for (Method m : c.getDeclaredMethods()) {
            System.out.println(m.getName()+",参数个数:"+m.getParameterCount()+"返回值类型:"+m.getReturnType());
        }
        //2 获取指定的方法
        Method m2 = c.getDeclaredMethod("setX", int.class);
        System.out.println(m2);
        //3 获得构造方法   //getDeclaredConstructors包含自动添加的默认无参构造。
        //getConstructors 不包含自动添加的默认无参构造
        for (Constructor<?> con : c.getDeclaredConstructors()) {
            System.out.println(con);
        }
    }

    private static void class类的加载() throws ClassNotFoundException {
        //主动加载
        Class c1 = Class.forName("cn.gok.reflect.A");
        System.out.println("-----------");
        //被动加载
        Class<A> c2 = A.class;
        Class<? extends A> c3 = new A().getClass();
        //证明唯一性(主动加载和被动加载得到的结果相同)
        System.out.println(c1 == c2);
        System.out.println(c2 == c3);
    }
}
